package Admin

import (
	"FromNowOnPromote/博客系统/Tblog/controllers"
	"FromNowOnPromote/博客系统/Tblog/models"
	"FromNowOnPromote/博客系统/Tblog/models/Admin"
	"encoding/json"
)

type AdminRegister struct {
	controllers.MainController
}

type AdminRegisterForm struct {
	Uuid     string
	UserName string
	Password string
}

func (this *AdminRegister) Post() {
	//解析表单
	var arf AdminRegisterForm
	r := this.Ctx.Request
	decoder := json.NewDecoder(r.Body)
	err1 := decoder.Decode(&arf)
	if err1 != nil {
		body := map[string]interface{}{"code": 300}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}
	//管理员信息插表
	db := models.ConnDb()
	defer db.Close()
	t := Admin.TbAdmin{UserId: arf.Uuid, Username: arf.UserName, Password: arf.Password}
	err2 := db.Create(&t).Error
	if err2 != nil {
		body := map[string]interface{}{"code": 301}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}
	body := map[string]interface{}{"code": 200, "data": "注册成功"}
	this.Data["json"] = body
	this.ServeJSON()
	return
}
